import java.io.File;
import java.io.IOException;
import java.util.Locale;

import jxl.CellView;
import jxl.Workbook;
import jxl.WorkbookSettings;
import jxl.format.UnderlineStyle;
import jxl.write.Formula;
import jxl.write.Label;
import jxl.write.Number;
import jxl.write.WritableCellFormat;
import jxl.write.WritableFont;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
import jxl.write.biff.RowsExceededException;


public class WriteExcel1 {

	public static WriteExcel1 instance = new WriteExcel1();
	
	private WritableCellFormat timesBoldUnderline;
	private WritableCellFormat times;
	private String inputFile;
	File file;
	WorkbookSettings wbSettings;
	WritableWorkbook workbook;
	WritableSheet excelSheet;
	WritableFont times10pt;
	WritableFont times10ptBoldUnderline;
	
	CellView cv;

	private static int count = 0;
	
	
public void setOutputFile(String inputFile) {

		 this.inputFile = inputFile;
		 file = new File(inputFile);
		 wbSettings = new WorkbookSettings();
		 wbSettings.setLocale(new Locale("en", "EN"));
		 
		 try {
			workbook = Workbook.createWorkbook(file, wbSettings);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		 workbook.createSheet("Report", 0);
		 excelSheet = workbook.getSheet(0);	 
		 
		// Lets create a times font
		 times10pt = new WritableFont(WritableFont.TIMES, 10);
		// Define the cell format
		 times = new WritableCellFormat(times10pt);
		 
		// Lets automatically wrap the cells
		try {
			times.setWrap(true);
		} catch (WriteException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		// Create create a bold font with unterlines
		times10ptBoldUnderline = new WritableFont(
		WritableFont.TIMES, 10, WritableFont.BOLD, false,
		UnderlineStyle.SINGLE);	
		
		
		timesBoldUnderline = new WritableCellFormat(times10ptBoldUnderline);
		// Lets automatically wrap the cells
		try {
			timesBoldUnderline.setWrap(true);
		} catch (WriteException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}		
		
		CellView cv = new CellView();
		cv.setFormat(times);
		cv.setFormat(timesBoldUnderline);
		cv.setAutosize(true);
	}

	public void write(Generation gen) throws IOException, WriteException {
		//File file = new File(inputFile);
		

		//createLabel(excelSheet);
		
		if(count < ( Data.numberOfGenerations + 1)){
			createContent(excelSheet, gen);
			
		}
		

		if(count == (Data.numberOfGenerations+1)){
			System.out.println("before close");
			workbook.write();
			workbook.close();
		}
		
	}

	
	
	private void createContent(WritableSheet sheet, Generation gen) throws WriteException,
			RowsExceededException {
		// Lets create a times font
		//WritableFont times10pt = new WritableFont(WritableFont.TIMES, 10);
		// Define the cell format


		CellView cv = new CellView();
		cv.setFormat(times);
		cv.setFormat(timesBoldUnderline);
		cv.setAutosize(true);

		//calculate the average of the time in prison
		double sum = 0;
		double average; 
		for(int i=0; i<gen.chromosomeList.size(); i++){
			sum+=gen.chromosomeList.get(i).timeInPrison;
		}
		
		average = sum/gen.chromosomeList.size();

		// First row - the average of the chromosom

		addNumber(sheet, count, 0, average );
		// Second row - the best chromosom
		addNumber(sheet, count, 1, gen.chromosomeList.get(gen.chromosomeList.size()-1).timeInPrison );
			
			
		count++;

	}


	private void addNumber(WritableSheet sheet, int column, int row,
			double double1) throws WriteException, RowsExceededException {
		Number number;
		number = new Number(column, row, double1, times);
		sheet.addCell(number);
	}

	
	/*
	
	public static void main(String[] args) throws WriteException, IOException {
		WriteExcel1 test = new WriteExcel1();
		test.setOutputFile("e:/programs/eclipse/Workspace/prisonerDilem/lars.xls");
		test.write();
		System.out
				.println("Please check the result file under e:/temp/lars.xls ");
	}
	*/
}
